<?php namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;

class Test extends BaseController{
	use ResponseTrait;
	private $_model;
	public function __construct(){
		$this->_model=new \App\Models\Test();
	}
	/**
	 * 列表
	 */
	public function index(){
		$page=$this->request->getGet('page', FILTER_VALIDATE_INT)?:1;
		$max=$this->request->getGet('max', FILTER_VALIDATE_INT)?:10;
		$result=$this->_model->list($page, $max);
		return $this->respond(['count'=>$result[0], 'list'=>$result[1]]);
	}
	/**
	 * 添加
	 */
	public function create(){
		$title=$this->request->getPost('title')?:'';
		$content=$this->request->getPost('content')?:'';
		$result=$this->_model->add(['title'=>$title, 'content'=>$content]);
		return $this->respond(['id'=>$result], 201);
	}
	/**
	 * 详情
	 * @param $id
	 */
	public function show($id){
		$result=$this->_model->get1($id);
		return $this->respond($result);
	}
	/**
	 * 更新
	 * @param $id
	 */
	public function update($id){
		$raw=$this->request->getRawInput();
		$title=$raw['title']??'';
		$content=$raw['content']??'';
		$result=$this->_model->edit($id, ['title'=>$title, 'content'=>$content]);
		return $this->respond('', 204);
	}
	/**
	 * 删除
	 * @param $id
	 * @return mixed
	 */
	public function delete($id){
		$this->_model->del($id);
		return $this->respond('', 204);
	}
}
